package com.shenhonghui.photo.component;

import com.shenhonghui.photo.ResourceTable;
import com.shenhonghui.photo.adapter.DevicesListAdapter;
import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ListContainer;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.CommonDialog;
import ohos.agp.window.service.WindowManager;
import ohos.app.Context;
import ohos.distributedschedule.interwork.DeviceInfo;
import ohos.distributedschedule.interwork.DeviceManager;

import java.util.ArrayList;
import java.util.List;

public class SelectDeviceDialog {
    private static final int WIDTH = 800;
    private static final int HEIGHT = 800;
    private static final int INIT_SIZE = 10;
    private CommonDialog commonDialog;
    private List<DeviceInfo> deviceIfs = new ArrayList<>(INIT_SIZE);

    /**
     * dialog select item listener
     *
     * @since 2021-02-25
     */
    public interface SelectResultListener {
        /**
         * select call back
         *
         * @param deviceInfo device info
         */
        void callBack(DeviceInfo deviceInfo);
    }

    /**
     * SelectDeviceDialog
     *
     * @param context context
     * @param callBack callBack
     * @since 2021-02-25
     */
    public SelectDeviceDialog(Context context, SelectResultListener callBack) {
        getDevices(context, callBack);
    }

    private void getDevices(Context context, SelectResultListener callBack) {
        if (deviceIfs.size() > 0) {
            deviceIfs.clear();
        }
        List<DeviceInfo> deviceInfos = DeviceManager.getDeviceList(DeviceInfo.FLAG_GET_ONLINE_DEVICE);
        deviceIfs.addAll(deviceInfos);
        initView(context, deviceIfs, callBack);
    }

    private void initView(Context context, List<DeviceInfo> devices, SelectResultListener listener) {
        commonDialog = new CommonDialog(context);
        commonDialog.setAlignment(LayoutAlignment.CENTER);
        Component dialogLayout = LayoutScatter.getInstance(context)
                .parse(ResourceTable.Layout_dialog_select_device, null, false);
        commonDialog.setSize(WIDTH, HEIGHT);
        commonDialog.setAutoClosable(true);
        commonDialog.setContentCustomComponent(dialogLayout);
        if (dialogLayout.findComponentById(ResourceTable.Id_list_devices) instanceof ListContainer) {
            ListContainer devicesListContainer = (ListContainer) dialogLayout.findComponentById(ResourceTable.Id_list_devices);
            DevicesListAdapter devicesListAdapter = new DevicesListAdapter(devices, context);
            devicesListContainer.setItemProvider(devicesListAdapter);
            devicesListContainer.setItemClickedListener((listContainer, component, position, ll) -> {
                listener.callBack(devices.get(position));
                commonDialog.hide();
                commonDialog.remove();
                WindowManager.getInstance().getTopWindow().get().setStatusBarVisibility(Component.INVISIBLE);     //设置状态栏不显示
            });
        }
        dialogLayout.findComponentById(ResourceTable.Id_cancel).setClickedListener(component -> {
            commonDialog.hide();
            commonDialog.remove();
            WindowManager.getInstance().getTopWindow().get().setStatusBarVisibility(Component.INVISIBLE);     //设置状态栏不显示
        });
    }

    /**
     * show dialog
     */
    public void show() {
        commonDialog.show();
    }

}
